<?php

namespace Modules\Admin\Http\Controllers;

use App\Common\Lib\Validate;
use Illuminate\Validation\Rule;
use Validator;
use App\Services\AdminService;
use Illuminate\Support\Facades\Auth;

class AdminController extends Controller
{
    protected $adminService;

    public function __construct(AdminService $adminService)
    {
        $this->adminService = $adminService;
    }

    /**
     * @SWG\Get(
     *     path="/admin/admin/captcha",
     *     summary="获取验证码",
     *     tags={"Admin/admin"},
     *     description="",
     *     produces={"application/json"},
     *     @SWG\Response(
     *         response=200,
     *         description="code=0：操作成功；其他操作失败，message中有错误信息"
     *     )
     * )
     */
    public function captcha()
    {
        try {
            $res = app('captcha')->create('default', true);

            return $this->ajaxReturn(0, '', $res);
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e->getMessage());
        }
    }

    /**
     * @SWG\Post(
     *     path="/admin/admin/sendSms",
     *     summary="发送短信",
     *     tags={"Admin/admin"},
     *     description="",
     *     produces={"application/json"},
     *     @SWG\Parameter(
     *         name="mobile",
     *         in="query",
     *         description="手机号",
     *         required=true,
     *         type="string",
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="code=0：操作成功；其他操作失败，message中有错误信息"
     *     )
     * )
     */
    public function sendSms()
    {
        try {
            $mobile = request('mobile');
            if (!Validate::isMobile($mobile)) {
                return $this->ajaxReturn(1002, '手机号错误');
            }

            return $this->ajaxReturn();
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e->getMessage());
        }
    }

    /**
     * @SWG\Post(
     *     path="/admin/admin/getArr",
     *     summary="获取定义的数组",
     *     tags={"Admin/admin"},
     *     description="",
     *     produces={"application/json"},
     *     @SWG\Parameter(
     *         name="arr_name",
     *         in="query",
     *         description="数组的名称（数组）",
     *         required=true,
     *         type="string",
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="code=0：操作成功；其他操作失败，message中有错误信息"
     *     )
     * )
     */
    public function getArr()
    {
        try {
            $res = [];
            $arr_name = request('arr_name');
            foreach ($arr_name as $name) {
                $res[$name] = call_user_func($name.'Arr');
            }

            return $this->ajaxReturn(0, '', $res);
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e->getMessage());
        }
    }

    /**
     * @SWG\Post(
     *     path="/admin/admin/getList",
     *     summary="员工列表",
     *     tags={"Admin/admin"},
     *     description="",
     *     produces={"application/json"},
     *     security={{"Bearer":{}}},
     *     @SWG\Response(
     *         response=200,
     *         description="code=0：操作成功；其他操作失败，message中有错误信息"
     *     )
     * )
     */
    public function getList()
    {
        try {
            $per_page = request('per_page', 20);

            if ($res = $this->adminService->getList(request('search', []), $per_page)) {
                return $this->ajaxReturn(0, '', $res);
            }

            return $this->ajaxReturn(1001);
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e->getMessage());
        }
    }

    /**
     * @SWG\Post(
     *     path="/admin/admin/detail/{id}",
     *     summary="员工详情",
     *     tags={"Admin/admin"},
     *     description="",
     *     produces={"application/json"},
     *     security={{"Bearer":{}}},
     *     @SWG\Response(
     *         response=200,
     *         description="code=0：操作成功；其他操作失败，message中有错误信息"
     *     )
     * )
     */
    public function detail($id)
    {
        try {
            if ($res = $this->adminService->detail($id)) {
                return $this->ajaxReturn(0, '', $res);
            }

            return $this->ajaxReturn(1001);
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e->getMessage());
        }
    }

    /**
     * @SWG\Post(
     *     path="/admin/admin/add",
     *     summary="添加员工",
     *     tags={"Admin/admin"},
     *     description="",
     *     produces={"application/json"},
     *     security={{"Bearer":{}}},
     *     @SWG\Response(
     *         response=200,
     *         description="code=0：操作成功；其他操作失败，message中有错误信息"
     *     )
     * )
     */
    public function add()
    {
        try {
            $requestData = request()->all();
            $validator = Validator::make($requestData, [
                'username' => 'required|unique:admins',
                'user_type' => 'required',
                'password' => 'min:6|max:20',
            ]);

            if ($validator->fails()) {
                return $this->ajaxReturn(1002, $validator->errors()->first());
            }

            if ($this->adminService->add($requestData)) {
                return $this->ajaxReturn();
            }

            return $this->ajaxReturn(1001);
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e->getMessage());
        }
    }

    /**
     * @SWG\Post(
     *     path="/admin/admin/edit/{uuid}",
     *     summary="编辑员工",
     *     tags={"Admin/admin"},
     *     description="",
     *     produces={"application/json"},
     *     security={{"Bearer":{}}},
     *     @SWG\Response(
     *         response=200,
     *         description="code=0：操作成功；其他操作失败，message中有错误信息"
     *     )
     * )
     */
    public function edit($id)
    {
        try {
            $requestData = request()->all();
            $validator = Validator::make($requestData, [
                'password' => 'required|min:6|max:20',
            ]);

            if ($validator->fails()) {
                return $this->ajaxReturn(1002, $validator->errors()->first());
            }

            if ($this->adminService->edit($id, $requestData)) {
                return $this->ajaxReturn();
            }

            return $this->ajaxReturn(1001);
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e->getMessage());
        }
    }

    /**
     * @SWG\Post(
     *     path="/admin/admin/remove",
     *     summary="删除员工",
     *     tags={"Admin/admin"},
     *     description="",
     *     produces={"application/json"},
     *     security={{"Bearer":{}}},
     *     @SWG\Response(
     *         response=200,
     *         description="code=0：操作成功；其他操作失败，message中有错误信息"
     *     )
     * )
     */
    public function remove($id)
    {
        try {
            if ($this->adminService->remove($id)) {
                return $this->ajaxReturn();
            }

            return $this->ajaxReturn(1001);
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e);
        }
    }

    /**
     * @SWG\Post(
     *     path="/admin/admin/resetPwd/{id}",
     *     summary="重置密码",
     *     tags={"Admin/admin"},
     *     description="",
     *     produces={"application/json"},
     *     security={{"Bearer":{}}},
     *     @SWG\Response(
     *         response=200,
     *         description="code=0：操作成功；其他操作失败，message中有错误信息"
     *     )
     * )
     */
    public function resetPwd($id)
    {
        try {
            if ($this->adminService->resetPwd($id)) {
                return $this->ajaxReturn();
            }

            return $this->ajaxReturn(1001);
        } catch (\Exception $e) {
            return $this->ajaxReturn(1001, $e->getMessage());
        }
    }
}
